package com.wgke.bluetooth.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment extends Fragment {
    protected Context context;
    protected View rootView;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
            initView(rootView);
            initData();
            initEvent();
        } else {
            // 防止重复加载
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    public abstract int getLayoutId();

    protected abstract void initView(View rootView);

    protected void initData() {
        // 默认空实现
    }

    protected void initEvent() {
        // 默认空实现
    }

    protected void releaseResources() {
        // 默认空实现
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 释放资源
        releaseResources();
    }
}
